package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.BookOrderInfo;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.PayAndBackParams;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.HashMap;
import java.util.UUID;
import org.json.JSONException;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/1/4 19:24
 */

public class AppointmentOrderDetailFragment extends BaseOrderDetailFragment {

  private static final int REQUEST_ALTER = 1;
  private static final int TYPE_SUBMIT = 2;
  private static final int TYPE_CANCEL = 3;
  private static final int TYPE_REMINDER = 4;
  private static final int TYPE_DELETE = 6;
  private static final int REQUEST_PAY = 7;

  public static AppointmentOrderDetailFragment newInstance(BookOrderInfo item) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_ITEM, item);
    AppointmentOrderDetailFragment fragment = new AppointmentOrderDetailFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    if (mItem == null || mItem.getDataStatus() == null) {
      return;
    }
    String mStatus = mItem.getDataStatus();
    if (mStatus.equals(BookOrderInfo.STATUS_NEW) || mStatus.equals(BookOrderInfo.STATUS_NEWBACK)) {
      inflater.inflate(R.menu.menu_delete_commit_alter, menu);
    }
    if (mStatus.equals(BookOrderInfo.STATUS_SUBMIT)) {
      inflater.inflate(R.menu.menu_cancel_order, menu);
    }
    if (mStatus.equals(BookOrderInfo.STATUS_CANCEL)) {
      inflater.inflate(R.menu.menu_delete, menu);
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_alter:
        AppointmentOrderUpdateActivity.newInstance(this, mActivity, mItem, REQUEST_ALTER);
        break;
      case R.id.menu_item_submit:
        //如果预约保证金不为0,区支付.否则直接提交
        String bookDeposit = tvBookDeposit.getText().toString().trim();
        //noinspection deprecation
        double bookDepositDouble = MyUtils.valueOfDouble(bookDeposit, 0);
        if (bookDepositDouble != 0) {
          PayAndBackParams params = new PayAndBackParams();
          params.setId(UUID.randomUUID().toString());
          params.setEditable(false);
          params.setMainId(mItem.getId());
          params.setPayMuch(String.valueOf(bookDepositDouble));
          params.setMoneyType(PayAndBackParams.MONEY_TYPE_GARRENTEE_FEE);
          params.setBookOrderNo(mItem.getBookNo());
          params.setOrderType(PayAndBackParams.ORDER_TYPE_ORDER);
          PayAndBackActivity.newInstance(this, PayAndBackActivity.class, params, REQUEST_PAY);
        } else {
          volleypostUpdateStatus(TYPE_SUBMIT);
        }

        break;
      case R.id.menu_item_delete:
        volleypostUpdateStatus(TYPE_DELETE);
        break;
      case R.id.menu_item_cancel:
        volleypostUpdateStatus(TYPE_CANCEL);
        break;
      //case R.id.menu_item_reminder:
      //    volleypostUpdateStatus(TYPE_REMINDER);
      //    break;
      default:
        break;
    }

    return super.onOptionsItemSelected(item);
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setTitleCustom(R.string.orderDetail);
  }

  private void volleypostUpdateStatus(int type) {
    if (mItem == null || mItem.getId() == null) return;
    String status = null;
    switch (type) {
      case TYPE_SUBMIT:
        status = "submit";
        break;
      case TYPE_CANCEL:
        status = "backOrder";
        break;
      case TYPE_DELETE:
        status = "delete";
        break;
      case TYPE_REMINDER:
        //status="delete";
        break;
      default:
        break;
    }
    if (TextUtils.isEmpty(status)) return;

    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_UPDATE_APPOINT_ORDER_STATUS);
    HashMap<String, String> params = new HashMap<>();
    params.put("ids", mItem.getId());
    params.put("memberCode", CurrentUser.newInstance(mActivity).getMemberCode());
    params.put("dataStatus", status);
    MyUtils.showDialogLoading(mActivity);
    mActivity.volleyPost(url, params, new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {
        ToastUtil.show("操作失败");
      }

      @Override public void onVolleyResponse(String arg0) throws JSONException {
        Base original = new Gson().fromJson(arg0, Base.class);
        if (original != null && original.getResult() == Base.SUCCESS) {
          ToastUtil.show("修改订单状态成功");
          mActivity.setResult(Activity.RESULT_OK);
          mActivity.finish();
        } else {
          ToastUtil.show("修改订单状态失败");
        }
      }
    });
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;
    switch (requestCode) {
      case REQUEST_ALTER:
        //volleyPostInit();
        mActivity.setResult(Activity.RESULT_OK);
        mActivity.finish();
        break;
      case REQUEST_PAY:
        volleypostUpdateStatus(TYPE_SUBMIT);
        break;
      default:
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }
}
